package cn.iocoder.yudao.module.bpm.component;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oaleavapplysub.OaLeavApplySubDO;
import cn.iocoder.yudao.module.bpm.service.oaapply.OaApplyService;

import cn.iocoder.yudao.module.bpm.service.oaleavapplysub.OaLeavApplySubService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName BpmOaApplySubCmp
 * @Description TODO
 * @Author dingyongze
 * @Date 2024/10/24 15:45
 * @Version 1.0
 */
@Component("LeaveCmp")
public class LeaveCmp extends BaseNodeComponent {
    @Autowired
    private OaLeavApplySubService oaLeavApplySubService;
    @Autowired
    private OaApplyService oaApplyService;
    @Override
    public void process() throws Exception {
        //设置总时长
        if (isCreateBefore() && isMain()) {
            String id = getId();
            List<OaLeavApplySubDO> list = oaLeavApplySubService.list(new LambdaQueryWrapper<OaLeavApplySubDO>()
                    .eq(OaLeavApplySubDO::getFk, id));
            float zsc = 0;
            for (OaLeavApplySubDO oaApplySubDO : list) {
                String sc = oaApplySubDO.getSc();
                if (StringUtils.isNotBlank(sc)) {
                    try {
                        zsc += Float.parseFloat(sc);
                    } catch (NumberFormatException e) {

                    }
                }
            }
            setValue("zsc", zsc);
        }
        if (isCreateBefore() && isSub()) {
            // 定义日期格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            JSONObject jsonObject =  this.getContextBean(JSONObject.class);
            String startDateStr = jsonObject.getString("start_date");
            String endDateStr = jsonObject.getString("end_date");
            LocalDateTime startDate = LocalDateTime.parse(startDateStr,formatter);
            LocalDateTime endDate = LocalDateTime.parse(endDateStr,formatter);
            // 确保 endDate 至少比 startDate 大 30 分钟
            if (!endDate.isAfter(startDate.plusMinutes(30))) {
                throw new ServiceException(500, "结束时间不能小于开始时间,时长最少要30分钟以上");
            }
            // 计算两个日期之间的分钟差
            Duration duration = Duration.between(startDate, endDate);
            long minutesDifference = duration.toMinutes();

            // 将分钟差换算成小时，并保留一位小数，四舍五入
            BigDecimal hoursDifference = BigDecimal.valueOf(minutesDifference).divide(BigDecimal.valueOf(60), 1, RoundingMode.HALF_UP);
            jsonObject.put("sc", hoursDifference.doubleValue());
        }

    }
}
